import { v4 as uuidv4 } from "uuid"

export class WordBankManager {
  constructor() {
    this.wordBanks = new Map()
    this.initializeDefaultWordBanks()
  }

  // 初始化默认题库
  initializeDefaultWordBanks() {
    // 动物题库
    this.createWordBank("动物", [
      "猫",
      "狗",
      "鸟",
      "鱼",
      "兔子",
      "老虎",
      "狮子",
      "大象",
      "熊猫",
      "猴子",
      "马",
      "牛",
      "羊",
      "猪",
      "鸡",
      "鸭",
      "蛇",
      "青蛙",
      "蝴蝶",
      "蜜蜂",
      "蚂蚁",
      "蜘蛛",
      "老鼠",
      "松鼠",
    ])

    // 水果题库
    this.createWordBank("水果", [
      "苹果",
      "香蕉",
      "橙子",
      "葡萄",
      "草莓",
      "西瓜",
      "桃子",
      "梨",
      "樱桃",
      "柠檬",
      "菠萝",
      "芒果",
      "猕猴桃",
      "石榴",
      "荔枝",
      "龙眼",
      "椰子",
      "榴莲",
      "火龙果",
      "百香果",
      "蓝莓",
      "黑莓",
      "覆盆子",
      "无花果",
    ])

    // 日常用品题库
    this.createWordBank("日常用品", [
      "杯子",
      "盘子",
      "筷子",
      "勺子",
      "刀",
      "叉子",
      "碗",
      "锅",
      "椅子",
      "桌子",
      "床",
      "枕头",
      "被子",
      "毛巾",
      "牙刷",
      "肥皂",
      "手机",
      "电脑",
      "电视",
      "冰箱",
      "洗衣机",
      "空调",
      "风扇",
      "灯",
    ])

    // 交通工具题库
    this.createWordBank("交通工具", [
      "汽车",
      "自行车",
      "摩托车",
      "公交车",
      "火车",
      "飞机",
      "船",
      "地铁",
      "出租车",
      "卡车",
      "救护车",
      "消防车",
      "警车",
      "校车",
      "拖拉机",
      "坦克",
      "直升机",
      "热气球",
      "滑板",
      "滑雪板",
      "轮船",
      "帆船",
      "潜水艇",
      "火箭",
    ])

    // 职业题库
    this.createWordBank("职业", [
      "医生",
      "护士",
      "老师",
      "学生",
      "警察",
      "消防员",
      "厨师",
      "服务员",
      "司机",
      "飞行员",
      "画家",
      "歌手",
      "演员",
      "作家",
      "程序员",
      "设计师",
      "律师",
      "会计",
      "销售员",
      "清洁工",
      "建筑工",
      "农民",
      "渔民",
      "邮递员",
    ])

    // 运动题库
    this.createWordBank("运动", [
      "跑步",
      "游泳",
      "篮球",
      "足球",
      "乒乓球",
      "羽毛球",
      "网球",
      "排球",
      "跳绳",
      "跳高",
      "跳远",
      "举重",
      "拳击",
      "跆拳道",
      "瑜伽",
      "舞蹈",
      "滑冰",
      "滑雪",
      "骑车",
      "爬山",
      "钓鱼",
      "射箭",
      "高尔夫",
      "保龄球",
    ])

    console.log(`初始化完成，共加载 ${this.wordBanks.size} 个默认题库`)
  }

  // 创建题库
  createWordBank(name, words) {
    const id = uuidv4()
    const wordBank = {
      id,
      name,
      words: [...words], // 复制数组
      createdAt: new Date().toISOString(),
      isDefault: true,
    }

    this.wordBanks.set(id, wordBank)
    console.log(`创建题库: ${name}，包含 ${words.length} 个词汇`)

    return wordBank
  }

  // 创建自定义题库
  createCustomWordBank(name, words) {
    const id = uuidv4()
    const wordBank = {
      id,
      name,
      words: [...words],
      createdAt: new Date().toISOString(),
      isDefault: false,
    }

    this.wordBanks.set(id, wordBank)
    console.log(`创建自定义题库: ${name}，包含 ${words.length} 个词汇`)

    return wordBank
  }

  // 获取所有题库
  getAllWordBanks() {
    return Array.from(this.wordBanks.values()).map((wb) => ({
      id: wb.id,
      name: wb.name,
      wordCount: wb.words.length,
      isDefault: wb.isDefault,
      createdAt: wb.createdAt,
    }))
  }

  // 获取特定题库
  getWordBank(id) {
    return this.wordBanks.get(id)
  }

  // 获取随机词汇
  getRandomWord(wordBankId = null) {
    let availableWords = []

    if (wordBankId && this.wordBanks.has(wordBankId)) {
      // 从指定题库获取
      availableWords = this.wordBanks.get(wordBankId).words
    } else {
      // 从所有题库获取
      this.wordBanks.forEach((wordBank) => {
        availableWords.push(...wordBank.words)
      })
    }

    if (availableWords.length === 0) {
      return "画画" // 默认词汇
    }

    const randomIndex = Math.floor(Math.random() * availableWords.length)
    return availableWords[randomIndex]
  }

  // 获取随机词汇列表
  getRandomWords(count = 3, wordBankId = null) {
    const words = []
    const usedWords = new Set()

    while (words.length < count) {
      const word = this.getRandomWord(wordBankId)
      if (!usedWords.has(word)) {
        words.push(word)
        usedWords.add(word)
      }

      // 防止无限循环
      if (usedWords.size >= this.getTotalWordCount()) {
        break
      }
    }

    return words
  }

  // 获取总词汇数
  getTotalWordCount() {
    let total = 0
    this.wordBanks.forEach((wordBank) => {
      total += wordBank.words.length
    })
    return total
  }

  // 删除题库
  deleteWordBank(id) {
    const wordBank = this.wordBanks.get(id)
    if (wordBank && !wordBank.isDefault) {
      this.wordBanks.delete(id)
      return true
    }
    return false
  }

  // 更新题库
  updateWordBank(id, name, words) {
    const wordBank = this.wordBanks.get(id)
    if (wordBank && !wordBank.isDefault) {
      wordBank.name = name
      wordBank.words = [...words]
      wordBank.updatedAt = new Date().toISOString()
      return wordBank
    }
    return null
  }
}
